﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;

public partial class System_Other_IndexImageList : BasePage
{
    t_indeximgBLL bll = new t_indeximgBLL();

    #region 属性
    private string StrWhere
    {
        get
        {
            return ViewState["StrWhere"] == null ? "" : ViewState["StrWhere"].ToString();
        }
        set
        {
            ViewState["StrWhere"] = value;
        }
    }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");

        if (!IsPostBack)
        {
            //绑定数据
            BindRepeaterData("1=1");
        }
    }

    #endregion

    #region 数据绑定
    private void BindRepeaterData(string swhere)
    {
        UtilityBLL dbu = new UtilityBLL();
        //绑定数据源
        RepeaterList.DataSource = dbu.GetListByPage("t_indeximg", "*", "1=1", "OrderNo", 0, 1, 20);
        RepeaterList.DataBind();
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label validText = (Label)e.Item.FindControl("ValidText");
            LinkButton Lbvalid = (LinkButton)e.Item.FindControl("BtnOperate");

            Lbvalid.Text = "修改";
            //设置启用禁用的显示文本
            if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
            {
                validText.Text = "禁用";
                validText.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                validText.Text = "启用";
                validText.ForeColor = System.Drawing.Color.Green;
            }
        }
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Valid")
        {
            #region 修改状态
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                t_indeximg model = new t_indeximg();
                model = bll.GetModel(id);
                model.Valid = model.Valid == 0 ? 1 : 0;
                bll.Update(model);

                //刷新列表
                BindRepeaterData(StrWhere);
            }
            catch (Exception ex)
            {
                base.Alert(ex.ToString());
            }
            #endregion
        }
        else if (e.CommandName == "Modify")
        {
            #region 编辑
            Label lab_Title = (Label)e.Item.FindControl("lab_Title");
            TextBox txt_Title = (TextBox)e.Item.FindControl("txt_Title");
            lab_Title.Visible = false;
            txt_Title.Visible = true;

            Label lab_LinkHref = (Label)e.Item.FindControl("lab_LinkHref");
            TextBox txt_LinkHref = (TextBox)e.Item.FindControl("txt_LinkHref");
            lab_LinkHref.Visible = false;
            txt_LinkHref.Visible = true;

            Label lab_OrderNo = (Label)e.Item.FindControl("lab_OrderNo");
            TextBox txt_OrderNo = (TextBox)e.Item.FindControl("txt_OrderNo");
            lab_OrderNo.Visible = false;
            txt_OrderNo.Visible = true;

            LinkButton lbtn_Modify = (LinkButton)e.Item.FindControl("lbtn_Modify");
            LinkButton lbtn_Update = (LinkButton)e.Item.FindControl("lbtn_Update");
            LinkButton lbtn_Cancel = (LinkButton)e.Item.FindControl("lbtn_Cancel");
            LinkButton lbtn_Delete = (LinkButton)e.Item.FindControl("lbtn_Delete");
            lbtn_Modify.Visible = false;
            lbtn_Update.Visible = true;
            lbtn_Cancel.Visible = true;
            lbtn_Delete.Visible = false;

            Button btn_ImgSrc = (Button)e.Item.FindControl("btn_ImgSrc");
            btn_ImgSrc.Visible = true;
            #endregion
        }
        else if (e.CommandName == "Cancel")
        {
            #region 取消编辑
            Label lab_Title = (Label)e.Item.FindControl("lab_Title");
            TextBox txt_Title = (TextBox)e.Item.FindControl("txt_Title");
            lab_Title.Visible = true;
            txt_Title.Visible = false;

            Label lab_LinkHref = (Label)e.Item.FindControl("lab_LinkHref");
            TextBox txt_LinkHref = (TextBox)e.Item.FindControl("txt_LinkHref");
            lab_LinkHref.Visible = true;
            txt_LinkHref.Visible = false;

            Label lab_OrderNo = (Label)e.Item.FindControl("lab_OrderNo");
            TextBox txt_OrderNo = (TextBox)e.Item.FindControl("txt_OrderNo");
            lab_OrderNo.Visible = true;
            txt_OrderNo.Visible = false;

            LinkButton lbtn_Modify = (LinkButton)e.Item.FindControl("lbtn_Modify");
            LinkButton lbtn_Update = (LinkButton)e.Item.FindControl("lbtn_Update");
            LinkButton lbtn_Cancel = (LinkButton)e.Item.FindControl("lbtn_Cancel");
            LinkButton lbtn_Delete = (LinkButton)e.Item.FindControl("lbtn_Cancel");
            lbtn_Modify.Visible = true;
            lbtn_Update.Visible = false;
            lbtn_Cancel.Visible = false;
            lbtn_Delete.Visible = true;

            Button btn_ImgSrc = (Button)e.Item.FindControl("btn_ImgSrc");
            btn_ImgSrc.Visible = false;
            #endregion
        }
        else if (e.CommandName == "Update")
        {
            #region 保存

            Label lab_Title = (Label)e.Item.FindControl("lab_Title");
            TextBox txt_Title = (TextBox)e.Item.FindControl("txt_Title");
            lab_Title.Visible = true;
            txt_Title.Visible = false;

            Label lab_LinkHref = (Label)e.Item.FindControl("lab_LinkHref");
            TextBox txt_LinkHref = (TextBox)e.Item.FindControl("txt_LinkHref");
            lab_LinkHref.Visible = true;
            txt_LinkHref.Visible = false;

            Label lab_OrderNo = (Label)e.Item.FindControl("lab_OrderNo");
            TextBox txt_OrderNo = (TextBox)e.Item.FindControl("txt_OrderNo");
            lab_OrderNo.Visible = true;
            txt_OrderNo.Visible = false;

            LinkButton lbtn_Modify = (LinkButton)e.Item.FindControl("lbtn_Modify");
            LinkButton lbtn_Update = (LinkButton)e.Item.FindControl("lbtn_Update");
            LinkButton lbtn_Cancel = (LinkButton)e.Item.FindControl("lbtn_Cancel");
            LinkButton lbtn_Delete = (LinkButton)e.Item.FindControl("lbtn_Cancel");
            lbtn_Modify.Visible = true;
            lbtn_Update.Visible = false;
            lbtn_Cancel.Visible = false;
            lbtn_Delete.Visible = true;

            Button btn_ImgSrc = (Button)e.Item.FindControl("btn_ImgSrc");
            btn_ImgSrc.Visible = false;

            HiddenField hf_ImaSrc = (HiddenField)e.Item.FindControl("hf_ImaSrc");
            //hf_ImaSrc.Value

            int id = Convert.ToInt32(e.CommandArgument);

            if (hf_ImaSrc.Value.Length <= 0)
            {
                base.Alert("请选择图片！");
                return;
            }
            int orderno = 0;
            int.TryParse(txt_OrderNo.Text.Trim(), out orderno);
            t_indeximg model = new t_indeximg();

            model = bll.GetModel(id);
            model.Title = txt_Title.Text.Trim();
            model.ImgSrc = hf_ImaSrc.Value;
            model.LinkHref = txt_LinkHref.Text.Trim();
            model.OrderNo = orderno;

            try
            {
                bll.Update(model);

                //刷新列表
                BindRepeaterData(StrWhere);

                base.Alert("操作成功！");
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
            }
            #endregion
        }
        else if (e.CommandName == "Delete")
        {
            #region 删除
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                t_bannerBLL bll = new t_bannerBLL();
                bll.Delete(id);

                //刷新列表
                BindRepeaterData(StrWhere);
            }
            catch (Exception ex)
            {
                base.Alert("操作失败！");
            }
            #endregion
        }
    }
    #endregion

    #region 新增事件
    protected void lbtn_Add_Click(object sender, EventArgs e)
    {
        if (this.hf_ImaSrc.Value.Length <= 0)
        {
            base.Alert("请选择图片！");
            return;
        }
        int orderno = 0;
        int.TryParse(this.txt_OrderNo.Text.Trim(), out orderno);
        t_indeximg model = new t_indeximg();
        model.ImgSrc = this.hf_ImaSrc.Value;
        model.LinkHref = this.txt_LinkHref.Text.Trim();
        model.OrderNo = orderno;
        model.Title = this.txt_Title.Text.Trim();
        model.Valid = 1;

        try
        {
            bll.Add(model);
            
            //刷新列表
            BindRepeaterData(StrWhere);

            this.hf_ImaSrc.Value = "";
            this.txt_LinkHref.Text = "";
            this.txt_OrderNo.Text = "";
            this.txt_Title.Text = "";

            base.Alert("操作成功！");
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
        }
    }
    #endregion
}
